﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using GeoFinderClientApp.ViewModel;
using GeoFinderClientApp.Model;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace GeoFinderClientApp.View
{
    public partial class FriendsManager : PhoneApplicationPage
    {
        private ViewModels vm;
        private IEnumerable<Friend> Friends;
        public FriendsManager()
        {
            InitializeComponent();
            vm = new ViewModels();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            vm.GetFriends();
            Friends = (from friend in vm.AllFriends select friend.GetCopy()).ToList<Friend>();
            rebind();
        }

        private void rebind()
        {
            WhiteList.DataContext = Friends.Where(f => f.IsWhiteVisible).OrderBy(f => f.Name);
            BlackList.DataContext = Friends.Where(f => !f.IsWhiteVisible).OrderBy(f => f.Name);
        }

        private void Apply_Click(object sender, EventArgs e)
        {
            var nw = new ObservableCollection<Friend>();
            foreach (var item in Friends)
            {
                nw.Add(item);
            }
            vm.AllFriends = nw;
            vm.SaveFriends();
            Web.WebCore.UpdateBlackAndWhite();
            MessageBox.Show("Change saved");
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
            else
            {
                NavigationService.Navigate(new Uri("/View/ApplicationsMain.xaml", UriKind.Relative));
            }
        }

        private void Move_Click(object sender, EventArgs e)
        {
            switch (PivotControls.SelectedIndex)
            {
                case 0:
                    var toRemove = WhiteList.Friends.SelectedItems;
                    foreach (var item in toRemove)
                    {
                        ((Friend)item).IsWhiteVisible = false;
                    }
                    break;
                case 1:
                    var toAdd = BlackList.Friends.SelectedItems;
                    foreach (var item in toAdd)
                    {
                        ((Friend)item).IsWhiteVisible = true;
                    }
                    break;
                default: break;
            }
            rebind();
        }

        private void SelectAll_Click(object sender, EventArgs e)
        {
            switch (PivotControls.SelectedIndex)
            {
                case 0:
                    var si = WhiteList.Friends.SelectedItems;
                    if (si.Count > 0)
                    {
                        si.Clear();
                    }
                    else
                    {
                        foreach (var item in WhiteList.DataContext as IEnumerable<Friend>)
                        {
                            si.Add(item);
                        }
                    }
                    break;
                case 1:
                    si = BlackList.Friends.SelectedItems;
                    if (si.Count > 0)
                    {
                        si.Clear();
                    }
                    else
                    {
                        foreach (var item in BlackList.DataContext as IEnumerable<Friend>)
                        {
                            si.Add(item);
                        }
                    }
                    break;
                default: break;
            }
        }

    }
}